package com.example.a02back.controller;

import cn.hutool.core.date.DatePattern;
import com.example.a02back.common.APIResponse;
import com.example.a02back.service.impl.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName PublishController
 * @Description
 * @Author SimpChaos
 * @Date 2022/7/8 15:03
 * @Version 1.0
 **/
@RestController
@ResponseBody
@Controller
@RequestMapping("/pub")
public class PublishController {

    @Resource
    private UserServiceImpl userService;
    @Resource
    private TradeServiceImpl tradeService;
    @Resource
    private AuthServicelmpl auth;
    @Resource
    private CardServiceImpl cardService;
    @Resource
    private TopicServiceImpl topicService;


    /**
     * @Description: 发布悬赏令或商品
     * @Author: SimpChaos
     * @Date: 2022/7/12 16:39
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/trade")
    public APIResponse<?> publish(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String title = httpServletRequest.getParameter("title");
        String sid = httpServletRequest.getParameter("sid");
        String info = httpServletRequest.getParameter("info");
        Integer type = Integer.parseInt(httpServletRequest.getParameter("type"));
        Integer tradetype = Integer.parseInt(httpServletRequest.getParameter("tradeType"));
        String deadline = httpServletRequest.getParameter("deadline");
        Integer price = Integer.parseInt(httpServletRequest.getParameter("price"));
        String result = tradeService.publish(uid,sid, title, info, type,tradetype,deadline,price);
        if (result.equals("false"))
            return APIResponse.fail("failed to publish");
        else return APIResponse.success("publish successfully", result);
    }


    /**
     * @Description: 上传商品图片
     * @Author: SimpChaos
     * @Date: 2022/7/12 16:38
     * @Param: [httpServletRequest, httpServletResponse, file]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/Tradeimg")
    public APIResponse<?> publishImage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, MultipartFile file) throws Throwable {
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String sid = httpServletRequest.getParameter("sid");
        return APIResponse.success(tradeService.publishImage(file, uid, sid), null);
    }


/**
 * @Description: 添加卡片
 * @Author: SimpChaos
 * @Date: 2022/7/14 17:26
 * @Param: [httpServletRequest, httpServletResponse]
 * @Return: com.example.a02back.common.APIResponse<?>
 */
    @CrossOrigin
    @RequestMapping("/card")
    public APIResponse<?> publishCard(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String department = httpServletRequest.getParameter("department");
        String position = httpServletRequest.getParameter("position");
        return APIResponse.success("success",cardService.publish(uid,department,position));
    }


    /**
     * @Description: 发布活动
     * @Author: SimpChaos
     * @Date: 2022/7/15 10:02
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/topic")
    public APIResponse<?> publishTopic(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String topicId =httpServletRequest.getParameter("aid");
        String headline = httpServletRequest.getParameter("headline");
        String content = httpServletRequest.getParameter("content");
        String time = httpServletRequest.getParameter("time");
        Integer activeValue = Integer.parseInt(httpServletRequest.getParameter("activeValue"));
        String actionAddress = httpServletRequest.getParameter("actionAddress");
        String actionPosition = httpServletRequest.getParameter("actionPosition");


        return  APIResponse.success("success",topicService.publish(topicId,uid,headline,content,time,activeValue,actionAddress,actionPosition));
    }



    /**
     * @Description: 
     * @Author: SimpChaos 
     * @Date: 2022/7/21 7:44
     * @Param: [httpServletRequest, httpServletResponse, file]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/Topicimg")
    public APIResponse<?> publishTopicImage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, MultipartFile file) throws Throwable {
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String topicid = httpServletRequest.getParameter("aid");
        return APIResponse.success(topicService.publishImage(file, uid, topicid), null);
    }




















}
